package cmms.home.dao;

import java.sql.ResultSet;
import java.util.List;

import cmms.home.vo.PointBO;
import fab.web.mvc.dao.IDao;
import fab.web.mvc.dao.IRowHandler;

public class PointService {
	// 排序显示观察点(传感器)
	public PointBO[] LoadSortedPoints(IDao dao, int count) {
		PointBO[] result = new PointBO[count];
		
		List<PointBO> points = LoadPoints(dao);
		if (points != null) {
			for(PointBO point: points) {
				int index = (int)point.getId(); 
				if (index >= 0 && index < count) {
					result[index] = point; 
				}
			}
		}
		
		PointBO zeroPoint = new PointBO();
		
		for (int i = 0; i < count; i++) {
			if (result[i] == null) {
				result[i] = zeroPoint;
			}
		}
		
		return result;
	}
	
	// 读取传感器状态列表
	@SuppressWarnings("unchecked")
	public List<PointBO> LoadPoints(IDao dao) {
		List<PointBO> result = (List<PointBO>) dao.query("select sensId, sensUpdateTime, sensX, sensY, sensState from mms_sensor order by sensId", new IRowHandler(){
			@Override
			public Object processRow(ResultSet rs) throws Exception {
				PointBO result = new PointBO();
				int col = 0;

				result.setId(rs.getLong(++col));
				result.setUpdateTime(rs.getTimestamp(++col));
				result.setIntX(rs.getInt(++col));
				result.setIntY(rs.getInt(++col));
				result.setState(rs.getShort(++col));
				
				return result;
			}
			
		});
		
		return result;
	}
	
//	public List<PointBO> listLog(IDao dao) {
//		
//	}
}
